package com.yueya.basic.dept.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.dept.model.Dept;
import com.yueya.basic.dept.model.DeptBase;
import com.yueya.basic.dict.model.Dict;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.model.Tree;

public class DeptController extends BaseController {

	Map<String, Object> map = new HashMap<String, Object>();

	public void index() {
		render("/WEB-INF/views/basic/dept/dept_list.jsp");
	}

	public void selectDept() {
		render("/WEB-INF/views/basic/dept/select_dept.jsp");
	}

	/**
	 * easyui tree
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void listTree() {
		List list = new ArrayList();
		list.add(Dept.service.recursiveTree("000"));
		String json = JsonKit.toJson(list);
		renderJson(json);

	}

	public void getDatagrid() {
		map.clear();
		map.put("dept_level", 1);
		Record d = null;
		if (getPara("parentid") != null) {
			map.put("parentid", getPara("parentid"));
			d = Db.findFirst("select * from t_dept where dept_id = '" + getPara("parentid") + "'");
		} else {
			map.put("order_name", "dept_no");
			map.put("order", "asc");
		}

		if (getPara("name") != null) {
			map.put("name", getPara("name"));
		}
		Page<Record> page = Dept.service.pagination(getPage(), getRows(), map);
		List<Record> list = page.getList();
		if (d != null) {
			list.add(d);
		}

		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void doAdd() {
		String pid = this.getPara("pid");
		Dept dept = new Dept();
		dept.set("dept_parent_id", pid);
		this.setAttr("dept", dept);
		render("/WEB-INF/views/basic/dept/dept_edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");
		Dept dept = Dept.dao.findById(id);
		DeptBase deptExt = Dept.service.getDeptExtByDeptId(id);
		this.setAttr("dept", dept);
		this.setAttr("deptBase", deptExt);
		render("/WEB-INF/views/basic/dept/dept_edit.jsp");
	}

	public void save() {
		Dept dept = this.getModel(Dept.class);
		DeptBase deptBase = this.getModel(DeptBase.class);
		boolean bl = Dept.service.save(dept, deptBase);
		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("操作成功.");
		} else {
			j.setSuccess(false);
			j.setMsg("操作失败.");
		}
		this.renderJson(j);
	}

	public void doRemove() {
		String id = this.getPara("id");
		Dept dept = Dept.dao.findById(id);
		dept.set("del_flag", "1");
		boolean bl = dept.update();
		if (bl == true) {
			renderHtml("success");
		} else {
			renderHtml("fail");
		}
	}

	/**
	 * 全部删除
	 */
	public void doRemoves() {
		Boolean bl = false;
		String idsStr = this.getPara("ids");
		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			Dept dept = null;
			for (int i = 0; i < ids.length; i++) {
				if (!"".equals(ids[i]) && ids[i] != null) {
					dept = Dept.dao.findById(ids[i]);
					dept.set("del_flag", "1");
					if (dept.update()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void deptlist() {

		Dept top = Dept.dao._r().get(0);
		Tree root = new Tree();
		root.setId(top.getStr("dept_id"));
		root.setIconCls("icon-wlkj-organ");
		root.setPid(top.getStr("dept_parent_id"));
		root.setText(top.getStr("DEPT_NAME"));
		root.setChildren(Dept.service.toTreeList(Dept.service.getDeptsByParentId(top.getStr("dept_id"))));
		List<Tree> tree = new ArrayList<Tree>();
		tree.add(root);
		renderJson(tree);
	}

	public void deptlistType() {
		String topid = this.getPara("topid");
		Dept top = Dept.dao.findById(topid);
		Tree root = new Tree();
		root.setId(top.getStr("dept_id"));
		root.setIconCls("icon-wlkj-organ");
		root.setPid(top.getStr("dept_parent_id"));
		root.setText(top.getStr("DEPT_NAME"));
		root.setChildren(Dept.service.toTreeList(Dept.service.getDeptsByParentId(top.getStr("dept_id"))));

		List<Tree> tree = new ArrayList<Tree>();
		tree.add(root);
		renderJson(tree);
	}

	public void deptnodes() {
		renderJson(Dept.service.toTreeList(Dept.service.getDeptsByParentId(this.getPara(0))));
	}

	public void getOrgType() {

		String json = Dict.service.getJsonValues("org_type");

		this.renderJson(json);
	}

	// 显示部门数据
	public void showdepttree() {
		String user_id = this.getPara("user_id");
		String sql = "select dept_id from t_user_charge_dept where user_id = ?";
		List<Record> list = Db.find(sql, user_id);
		String strdepts = "";

		for (int i = 0; i < list.size(); i++) {
			strdepts += list.get(i).getStr("dept_id");

			if (i != (list.size() - 1)) {
				strdepts += ",";
			}
		}
		this.setAttr("depts", strdepts);
		this.setAttr("user_id", user_id);
		render("/WEB-INF/views/basic/dept/depttree.jsp");
	}

}
